class LoginController {

    def index = { redirect(action: "form")}

    def login = { LoginCommand form ->
        log.debug "attempting login - email: ${form.email} | pwd: ${form.password}"

        if(form.hasErrors()) {
            redirect(action: "form")
        } else {
            User user = User.findByEmailAndPassword(form.email, form.password)
            if(user) {
                log.debug("${user} authenticated!")
                session.user_id = user.id
                if(user.firstName) {
                    session.user_firstName = user.firstName
                }
                if(user.hasWeddingProfile()) {
                    session.wedding_profile = true
                }
                redirect(controller: "profile", action: "dashboard")
            } else {
                flash.loginError = "Invalid email or password. Please try again"
                redirect(action: "form")
            }

        }

    }

    def form = {
        // placeholder for the login page
    }
}
